package hot.linklist;

public class A25 {

}



 class ListNode {
     int val;
     ListNode next;
    ListNode() {}
   ListNode(int val) { this.val = val; }
     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 }

class Solution {
    public ListNode reverseKGroup(ListNode head, int k) {
        if (head == null || k == 1) return head;
        int a=1;
        ListNode p=head;
        for(int i=0;i<k;i++){
            if(p==null)return head;
            p=p.next;
        }

        //翻转前k个
        ListNode node=reverse(head,k);
        head.next=reverseKGroup(p,k);
        return node;
    }

    public ListNode reverse(ListNode node,int k){
        ListNode pre=null;
        ListNode cur=node;
        int m=0;
        while(k-->0){
            ListNode next= cur.next;
            cur.next=pre;
            pre=cur;
            cur=next;
        }
        return pre;
    }
}

